package com.kk.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kk.model.enumbean.LevelEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author: kevin
 * @data: 2018-08-20 21:35
 * @desc:
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
public class Menu extends BaseEntity {

    @ManyToOne
    @JsonIgnore
    private Menu parent;

    @Column(nullable = false)
    @NonNull
    private String name;

    //    @ManyToMany(mappedBy = "menus", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
//    private List<Role> roles;
    //菜单等级
    private LevelEnum level;
    //同级排序
    private int sort;
    //菜单等级描述
    private String levelDesc;

    @Transient  //不在数据库生成字段
    private Set<Menu> childs;

    //菜单类型,默认为菜单
    private int menuType = 1;

    //前端路由根路径重定向
    private String redirect;
    //前端路由菜单路径
    @NonNull
    private String path;
    //前端路由菜单中文名称
    @NonNull
    private String engName;
    //前端组件路径
    private String componentPath;
    //前端路由所需meta
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
//    @JoinColumn(name="meta_id",referencedColumnName="id",nullable=false)
//    @NonNull
    private MenuMeta meta;

    public Menu() {

    }

    public Menu(String name, LevelEnum level, String levelDesc, String path, String engName, MenuMeta meta) {
        this.name = name;
        this.level = level;
        this.levelDesc = levelDesc;
        this.path = path;
        this.engName = engName;
        this.meta = meta;
    }

    public Menu(String name, LevelEnum level, String levelDesc, String path, String engName, String componentPath, MenuMeta meta, Menu menu) {
        this.name = name;
        this.level = level;
        this.levelDesc = levelDesc;
        this.path = path;
        this.engName = engName;
        this.componentPath = componentPath;
        this.meta = meta;
        this.parent = menu;
    }
}
